package org.blueoxygen.kukuruyuk.action.masterreceipe;

import java.sql.SQLException;
import org.blueoxygen.cimande.persistence.hibernate.HibernateSessionFactory;
import
org.blueoxygen.cimande.persistence.hibernate.HibernateSessionFactoryAware;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;


import org.blueoxygen.kukuruyuk.action.masterreceipe.MasterReceipeForm;
import org.blueoxygen.kukuruyuk.entity.MasterReceipe;
public class SearchMasterReceipe extends MasterReceipeForm
implements HibernateSessionFactoryAware {
    private HibernateSessionFactory hsf;
    private Session sess;
    private int maxPage, currPage, nextPage, prevPage = 0, page = 0;
    private int maxRowPerPage = 10;
    private String orderBy = "purchaseOrder";
    private int resultRows;
    public String execute(){
         sess = hsf.createSession();
         Criteria crit = sess.createCriteria(MasterReceipe.class);
         if(getMasterreceipe().getRealizeDate() != null
                 && !"".equalsIgnoreCase(getMasterreceipe().getRealizeDate())){
             crit.add(Expression.like("realizeDate", getMasterreceipe().getRealizeDate(),
                     MatchMode.ANYWHERE));
         }
         if(getMasterreceipe().getRealizeDate()!= null
                 && !"".equalsIgnoreCase(getMasterreceipe().getRealizeDate())){
             crit.add(Expression.like("vehicleNumber", getMasterreceipe().getRealizeDate(),
                     MatchMode.ANYWHERE));
         }
         
         if(getPo().getId() != null &&
                 !"".equalsIgnoreCase(getPo().getId())){
             crit.add(Expression.like("po", getPo()));
         }
         resultRows = crit.list().size();
         maxPage = resultRows / maxRowPerPage;
         prevPage = currPage - 1;
         nextPage = currPage + 1;
         page = currPage + 1;
         if (resultRows % maxRowPerPage == 0) maxPage = maxPage -1;
         setMasterreceipes(crit.addOrder(Order.asc(orderBy))
                 .setFirstResult(currPage * maxRowPerPage)
                 .setMaxResults(maxRowPerPage)
                 .list());
         try {
             hsf.endSession((sess));
             hsf.closeSession(sess);
         } catch (HibernateException e) {
             e.printStackTrace();
         } catch (SQLException e) {
             e.printStackTrace();
         }
         return SUCCESS;
    }
    public void setHibernateSessionFactory(HibernateSessionFactory hsf) {
         this.hsf = hsf;
    }
    public int getMaxPage() {
         return maxPage;
    }
    public void setMaxPage(int maxPage) {
         this.maxPage = maxPage;
    }
    public int getCurrPage() {
         return currPage;
    }
    public void setCurrPage(int currPage) {
         this.currPage = currPage;
    }
    public int getNextPage() {
         return nextPage;
    }
    public void setNextPage(int nextPage) {
         this.nextPage = nextPage;
    }
    public int getPrevPage() {
         return prevPage;
    }
    public void setPrevPage(int prevPage) {
         this.prevPage = prevPage;
    }
    public int getMaxRowPerPage() {
         return maxRowPerPage;
    }
    public void setMaxRowPerPage(int maxRowPerPage) {
         this.maxRowPerPage = maxRowPerPage;
    }
    public String getOrderBy() {
      return orderBy;
  }
  public void setOrderBy(String orderBy) {
      this.orderBy = orderBy;
  }
  public int getResultRows() {
      return resultRows;
  }
  public void setResultRows(int resultRows) {
      this.resultRows = resultRows;
  }
  public int getPage() {
      return page;
  }
  public void setPage(int page) {
      this.page = page;
  }
}